'use strict'

define(['jquery', 'bootstrap', 'select2', 'datetimepicker', 'datetimepickerCN',
        'shiftform', 'validate', 'validate.en'], function($) {
  
  return {
        
    init: function() {
        this.initEdit();
        this.initModal();
        this.refund();
        this.initInvoice();
        this.initShip();
        this.initDistributionChannel();
    },
    
    
    initEdit: function(){
        $(document).on("shown.bs.modal", "#remark", function(e) {
        var $form = $(e.currentTarget).find("form");
      
        $form.bt_validate();
      
        $form.shiftform({
            beforeSubmit: function(el) {
              $('form').trigger("validate.ajax.submit");
                   return $.bt_validate.result[$.bt_validate.form_id]
            }
        
        });
        
        })
        
    },
    
    initModal: function(){
        $(document).on('shown.bs.modal', '#close', function(e){

            $(e.currentTarget).find("form").shiftform()
            })
        
    },
    
    refund: function(){
        $(document).on("shown.bs.modal", "#refund", function(e) {
        var $form = $(e.currentTarget).find("form");
      
        $form.bt_validate();
      
        $form.shiftform({
            beforeSubmit: function(el) {
              $('form').trigger("validate.ajax.submit");
                   return $.bt_validate.result[$.bt_validate.form_id]
            }
        });
        
        })
        
    },
    
    initInvoice: function(){
        $(document).on("shown.bs.modal", "#invoice", function(e) {
        
        	$(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
  	        
  	        var $form = $(e.currentTarget).find("form")                          //bind shiftform
  	        $form.shiftform({
  	          beforeSubmit : function(el) {
  	            $form.trigger("validate.ajax.submit");
  	            return $.bt_validate.result[$.bt_validate.form_id]
  	          }
  	        })
        })
        
    },
    
    initShip: function(){
        $(document).on("shown.bs.modal", "#ship", function(e) {
        var $form = $(e.currentTarget).find("form");
      
        $form.bt_validate();
      
        $form.shiftform({
            beforeSubmit: function(el) {
              $('form').trigger("validate.ajax.submit");
                   return $.bt_validate.result[$.bt_validate.form_id]
            }
        });
        
        })
        
    },
    
    initDistributionChannel: function(){
        $(document).on("shown.bs.modal", "#distribution-channel", function(e) {
        
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
              
              var $form = $(e.currentTarget).find("form")                          //bind shiftform
              $form.shiftform({
                beforeSubmit : function(el) {
                  $form.trigger("validate.ajax.submit");
                  return $.bt_validate.result[$.bt_validate.form_id]
                }
              })
        })
        
    }
    
    
   }
})
